In this example when we tap on the Text View we will see the Text View move from its initial to the new position.
Initially we show Text View with offset = 0. When the Text is tapped we change offset to 100.
Since offset is State Variable, changing its value redraws the View and Text View is shown at new offset position.
Since we have added .animation Modifier to the Text View it will move into new position through an animation.
ContentView.swift
struct ContentView : View {
@State private var offset:CGFloat = 0 //Define Stata Variable offset
var body: some View {
Text("TEXT") //Add Text View
.offset(x: offset) //We will change offset Property
.onTapGesture { self.offset = 100 } //On Tap changes offset Property
.animation(Animation.easeOut(duration: 5)) //Define animation for when Property changes
}
}
Initial position Final position (after tapping on the Text)